// pages/home/home.js
// index.js
// 获取应用实例
const app = getApp()
const db = wx.cloud.database()
Page({
  data: {
    class_cur: 1,
    list: [],
    loadingHidden: false,
    searchKey: "", //监控搜索框输入信息
  },
  onLoad: function () {
          var that = this;
          db.collection('testkg').doc('store')
            .get().then(res => {
              const storeUp = res.data.kg;
              if (storeUp == true) {
                /*当store开关为true时，不更新缓存*/
                if (storeUp) {
                  try {
                    var t = wx.getStorageSync('list');
                    if (t) {
                      that.setData({
                        list: t,
                        loadingHidden: true
                      })
                    } else {
                      let p1 = new Promise((resolve, reject) => {
                        db.collection('goods').where({
                          disap: that.data.class_cur /*是否显示*/ /*没有就到服务器读取读取*/
                        }).get().then(res => {
                          wx.setStorageSync('list', res.data);
                          resolve(res.data);
                        })
                      })
                      Promise.all([p1]).then((result) => {
                        that.setData({
                          list: result[0],
                          loadingHidden: true
                        })
                      })
                    }
                  } catch (e) {}
                }
              } else {
                /*当store开关为false时，每次都清缓存，记住要关掉*/
                wx.removeStorage({
                  key: 'list',
                });
                let p1 = new Promise((resolve, reject) => {
                  db.collection('goods').where({
                    disap: that.data.class_cur /*是否显示*/ /*没有就到服务器读取读取*/
                  }).get().then(res => {
                    wx.setStorageSync('list', res.data);
                    resolve(res.data);
                  })
                })
                Promise.all([p1]).then((result) => {
                  that.setData({
                    list: result[0],
                    loadingHidden: true
                  })
                })
              }
            })  
  },
  searchInput: function (e) {
    // console.log(e)
    let value = e.detail.value //搜索框输入的信息
    this.setData({
      searchKey: value //监听搜索输入关键字信息
    })
  },
  //设置搜索规则
  search: function (e) {
    let searchKey = this.data.searchKey //监听搜索框输入的信息
    var value = wx.getStorageSync('list');
    if (searchKey == '') { //如果不输入任何字符直接搜索，返回所有
      this.setData({
        list: value
      })
      return
    }
    db.collection('goods').where({
       name: db.RegExp({
        regexp:searchKey,
        option: 'i'
      }) 
      })
      .get().then(res => { //获取查询到的信息
        //console.log(res)
        var t = res.data;
        if (res.data.length == 0) { //如果搜索信息在数据库中找不到
          wx.showModal({
            title: '',
            content: '无此产品！',
            showCancel: false,
            success(res) {}
          })
        } else {
          this.setData({
            list: t
          })
        }
      }).catch(res => {
        
      })
  },
  //设置弹窗规则
  showModal(e) {
    this.setData({ //设置搜索弹窗表头的字符
      modalName: e.currentTarget.dataset.target
    })
    this.search(e) //调用搜索函数
  },
  Toxq: function (e) {
    this.pageRouter.navigateTo({
      url: "../../pages/good/good?id=" + e.currentTarget.dataset.id
    })
  },
})